package fileProcessors.implementations.changeChar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import fileProcessors.FileProcessor;

public abstract class ChangeChar extends FileProcessor {

	private BufferedWriter outputWriter;
	private BufferedReader br;
	private BufferedWriter resultWriter;

	public ChangeChar() {
		
	}

	public ChangeChar(String processButton, String includeFilename, String excludeFilename) {
		super(processButton, includeFilename, excludeFilename);
	}

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) {
		try {
			resultWriter = new BufferedWriter(new FileWriter(outputFile));
			for (int i = 0; i < files.length; i++) {
				outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, files[i].getName()
						.substring(0, files[i].getName().length() - 4) + "_converted.txt")));
				br = new BufferedReader(new FileReader(files[i]));
				String line = "";
				while (line != null) {
					line = zeichenAendern(line);
					outputWriter.write(line);
					if (!line.equals("")) {
						outputWriter.newLine();
					}
					line = br.readLine();
				}
				outputWriter.close();
				br.close();
				resultWriter.write(files[i].getAbsolutePath() + " konvertiert.");
				resultWriter.newLine();
				// TODO Auto-generated catch block
			}
			
			resultWriter.close();
		} catch (IOException e) {
			
		}
	}

	protected abstract String zeichenAendern(String s);
}
